package com.shengqi.xinnongren.work.ui.goods.presenter;


import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.goods.model.ChoosePayTypeBean;
import com.shengqi.xinnongren.work.ui.goods.view.ChoosePayTypeView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/8/3.
 */
public class ChoosePayTypePresenter extends BasePresenter<ChoosePayTypeView> {
    public void getData(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getPayData(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                ChoosePayTypeBean bean = new Gson().fromJson(httpResult.data, ChoosePayTypeBean.class);
                getView().showPayInfo(bean);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }

    public void pay(String orderId, int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("type", type);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().pay(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                getView().showPayRes(httpResult);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }

    public void paySuccess(String orderId, int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("payment", type);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().paySuccess(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                getView().showPaySuccessRes(httpResult);
            } else
                getView().showRequestFailed(httpResult.message);
        });
    }
}
